
//
//  SEGPayLogYearDeatilViewController.m
//  ExcellentHome
//
//  Created by 刘山 on 16/12/14.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGBillHeaderViewCell.h"
#import "SEGHeadDescView.h"
#import "SEGPayLogDetailHeadView.h"
#import "SEGPayLogYearDeatilViewController.h"
#import "SEGNewBillManager.h"

@interface SEGPayLogYearDeatilViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) SEGPayLogDetailHeadView* payLogDetailHeadView;
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) SEGNewBillManager* billManager;
@property (nonatomic, strong) NSArray<SEGOwesBillDetailModel *> *detailModels;

@end

@implementation SEGPayLogYearDeatilViewController {
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initPayLogYearDeatil];

    [self loadData];
}

- (void)initPayLogYearDeatil
{
    self.title = @"记录详情";
    self.view.backgroundColor = kColor_bg;
    [self showCustomNavigationBackButton];

    [self.payLogDetailHeadView setFrame:CGRectMake(0, 0, LYScreenWidth(), 118)];
    self.payLogDetailHeadView.contentLabel.text = self.dataSource.payMethodName;
    self.payLogDetailHeadView.contentLabel2.text = [NSString stringWithFormat:@"%.2f元", [self.dataSource.payAmont floatValue] * MoneyRateChange];
    self.payLogDetailHeadView.contentLabel4.text = self.dataSource.payDate;
    self.payLogDetailHeadView.contentLabel6.text = self.dataSource.address;

    [self.view addSubview:self.tableView];

    [self.tableView registerClass:[SEGBillHeaderViewCell class] forCellReuseIdentifier:SEGBillHeaderViewCellIndentifier];

    self.tableView.tableHeaderView = self.payLogDetailHeadView;

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.tableView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
}

- (void)loadData
{

    NSDictionary* params = @{
        @"houseId" : self.houseId,
        @"paySerialNbr" : self.dataSource.paySerialNbr,
        @"custId" : self.custId,
        @"custType" : self.custType
    };

    WeakSelf;
    [self.billManager requestBillPaymentRecoedDetail:params finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
        weakSelf.detailModels = weakSelf.billManager.recordDetailModels;
        [weakSelf.tableView reloadData];
    }];
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.detailModels.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.detailModels[section].billDetail.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

    //组数据校验
    if (indexPath.section >= self.detailModels.count) {
        UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        return cell;
    }

    //行数据校验
    if (indexPath.row >= self.detailModels[indexPath.section].billDetail.count) {
        UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        return cell;
    }

    //头部样式
    SEGOwesMonthDetailModel* data_Row = self.detailModels[indexPath.section].billDetail[indexPath.row];
    SEGOwesBillDetailModel* data_Sec = self.detailModels[indexPath.section];

    //头部样式
    if (data_Row.type == SEGBillDetailDoLogicModelType_Head) {

        SEGBillHeaderViewCell* cell = [[SEGBillHeaderViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SEGBillHeaderViewCellIndentifier WithStype:SEGBillHeaderViewCellType_BillLog];
        [cell useBillDetailCellUI];
        cell.ContentLabel.text = @"项目";
        cell.ContentLabel2.text = @"费用(元)";
        return cell;
    }

    //展示数据样式
    if (data_Row.type == SEGBillDetailDoLogicModelType_Data) {

        SEGBillHeaderViewCell* cell = [[SEGBillHeaderViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SEGBillHeaderViewCellIndentifier WithStype:SEGBillHeaderViewCellType_PayLog_DataDeatil];
        [cell useBillDetailCellUI];
        //正常账单费用
        if (data_Row.feeType == SEGBillDetailDoLogicModelFeeType_Fee) {

            cell.ContentLabel.text = data_Row.feeItemTypeName;
            cell.ContentLabel2.text = [NSString stringWithFormat:@"%.2f", [data_Row.fee floatValue] * MoneyRateChange];
        }

        //账单违约金费用
        if (data_Row.feeType == SEGBillDetailDoLogicModelFeeType_LFee) {

            cell.ContentLabel.text = [NSString stringWithFormat:@"%@违约金", data_Row.feeItemTypeName];
            cell.ContentLabel2.text = [NSString stringWithFormat:@"%.2f", [data_Row.lfree floatValue] * MoneyRateChange];
        }
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
            [cell layoutIfNeeded];
        }

        return cell;
    }

    //展示尾部样式
    if (data_Row.type == SEGBillDetailDoLogicModelType_Foot) {

        SEGBillHeaderViewCell* cell = [[SEGBillHeaderViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SEGBillHeaderViewCellIndentifier WithStype:SEGBillHeaderViewCellType_PayLog_Footer];
        cell.ContentLabel.text = @"总计";
        cell.ContentLabel2.text = [NSString stringWithFormat:@"%.2f 元", [data_Sec.feeAmount floatValue] * MoneyRateChange];
        [cell useBillDetailCellUI];
        return cell;
    }

    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    return cell;
}

#pragma mark -UITableViewDelegate

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    //    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    //
    //        if (indexPath.row < self.billManager.logic_segbilldetailmodel.count) {
    //
    //            SEGBillDetailDoLogicModel* data = self.billManager.logic_segbilldetailmodel[indexPath.row];
    //
    //            //头部样式、尾部
    //            if (data.type == SEGBillDetailDoLogicModelType_Head || data.type == SEGBillDetailDoLogicModelType_Foot) {
    //                return 44.0;
    //            }
    //
    //            //展示数据
    //            return UITableViewAutomaticDimension;
    //        }
    //    }

    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return 100.0;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{

    //组数据校验
    if (section >= self.detailModels.count) {
        UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        return cell;
    }

    //头部样式
    SEGOwesBillDetailModel* data_Sec = self.detailModels[section];

    NSString* title = data_Sec.billingCycle;
    if (data_Sec.billingCycle.length == 6) {
        title = [NSString stringWithFormat:@"%@年%@月", [title substringToIndex:title.length - 2], [title substringFromIndex:4]];
    }

    SEGHeadDescView* head = [[SEGHeadDescView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 44)];
    head.contentLabel.text = title;
    [head setPosition:SEGHeadDescViewPosition_Center];

    return head;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

#pragma mark - lazy load

- (UITableView*)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (SEGNewBillManager *)billManager {
    if (!_billManager) {
        _billManager = [[SEGNewBillManager alloc] init];
    }
    return _billManager;
}

- (SEGPayLogDetailHeadView*)payLogDetailHeadView
{
    if (!_payLogDetailHeadView) {
        _payLogDetailHeadView = [[SEGPayLogDetailHeadView alloc] init];
    }
    return _payLogDetailHeadView;
}

- (SEGPayLogModel*)dataSource
{
    if (!_dataSource) {
        _dataSource = [[SEGPayLogModel alloc] init];
    }
    return _dataSource;
}
@end
